package servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import beans.ChatMessagebean;

import dao.Chatmessage;
import dao.User;

public class MessageManagerServlet extends HttpServlet {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    /**
     * The doGet method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to get.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	this.doPost(request, response);
    }

    /**
     * The doPost method of the servlet. <br>
     * 
     * This method is called when a form has its tag value method equals to
     * post.
     * 
     * @param request
     *            the request send by the client to the server
     * @param response
     *            the response send by the server to the client
     * @throws ServletException
     *             if an error occurred
     * @throws IOException
     *             if an error occurred
     */
    public void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {

	if (request.getQueryString() != null) {
	    String actionString = request.getParameter("action").trim();
	    if ("add".equals(actionString)) {
		String sourceString = ((User) request.getSession()
			.getAttribute("user")).getUserid();
		String destination = request.getParameter("des").trim();
		String context = request.getParameter("messageContext").trim();
		String type = request.getParameter("messageType").trim();
		if (context != null && !"".equals(context) && type != null
			&& !"".equals(type)) {
		    Chatmessage chatmessage = new Chatmessage();
		    chatmessage.setDestination(destination);
		    chatmessage.setSource(sourceString);
		    chatmessage.setReadflag("0");
		    chatmessage.setMessage(context);
		    chatmessage.setSendtime(new Date());
		    chatmessage.setMessagetype(type);
		    ChatMessagebean chatMessagebean = new ChatMessagebean();
		    if (chatMessagebean.save(chatmessage)) {
			request.setAttribute("success", "true");
		    } else {
			request.setAttribute("success", "false");
		    }
		    request.getRequestDispatcher(
			    "../common/sendMessage.jsp?destination="
				    + destination).forward(request, response);
		}
	    } else if ("reply".equals(actionString)) {
		String sourceString = ((User) request.getSession()
			.getAttribute("user")).getUserid();
		String destination = request.getParameter("des");
		String idStr = request.getParameter("messageid");
		String context = request.getParameter("messageContext").trim();
		if (context != null && !"".equals(context)) {
		    Chatmessage chatmessage = new Chatmessage();
		    chatmessage.setDestination(destination);
		    chatmessage.setSource(sourceString);
		    chatmessage.setReadflag("0");
		    chatmessage.setMessage(context);
		    chatmessage.setSendtime(new Date());
		    int id = Integer.parseInt(idStr);
		    ChatMessagebean chatMessagebean = new ChatMessagebean();
		    if (chatMessagebean.saveReply(chatmessage, id)) {
			request.getRequestDispatcher("../teacher/messages.jsp")
				.forward(request, response);
		    } else {
			request.setAttribute("success", "false");
			request.getRequestDispatcher(
				"../teacher/replyMessage.jsp?id=" + id).forward(
				request, response);
		    }
		}
	    }
	}
    }
}
